package cn.lyjuan.aliyun.mq.conf.java.dynamic.listener;

import cn.lyjuan.aliyun.mq.conf.java.dynamic.ADynamicBean;
import com.aliyun.openservices.ons.api.MessageListener;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

/**
 * 消息监听器
 * Created by chad on 2016/10/11.
 */
public class MQListenerDynamicImpl extends ADynamicBean
{
    public static final String NAME_SUFFIX = "MessageListener";

    /**
     * 消息监听器Bean类
     */
    private Class<? extends MessageListener> listenerClass;

    public MQListenerDynamicImpl(String baseName, Class<? extends MessageListener> listenerClass)
    {
        super(baseName, NAME_SUFFIX);
        this.listenerClass = listenerClass;
    }

    @Override
    public void register(DefaultListableBeanFactory beanFactory)
    {
        if (isContainsBean(beanFactory)) return;

        // 将属性加载到BeanDefinition中
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(listenerClass);
        builder.getBeanDefinition().setAttribute("id", this.getQuiteBeanName());
        builder.setLazyInit(false);// 启动则加载
        // 将Bean注册到SpringBeanFactory中
        beanFactory.registerBeanDefinition(this.getQuiteBeanName(), builder.getBeanDefinition());
    }
}
